/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ZoneConnectionsPropertyEditorPanel.java
 *
 * Created on Aug 13, 2011, 12:29:16 AM
 */
package pt.utl.ist.isr.livetrack.ui.propedit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.swing.DefaultComboBoxModel;
import org.openide.explorer.propertysheet.PropertyEnv;
import pt.utl.ist.isr.livetrack.server.LTServer;
import pt.utl.ist.isr.livetrack.server.interfaces.Zone;
import pt.utl.ist.isr.livetrack.ui.CameraNode;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class CameraZonePropertyEditorPanel extends javax.swing.JPanel {

    private final Zone zone;
    private final CameraNode camera;
    private final LTServer server;
    private final ArrayList<Zone> zones;

    CameraZonePropertyEditorPanel(PropertyEnv pe) {
        initComponents();
        camera = (CameraNode) pe.getBeans()[0];
        this.server = camera.getServer();
        this.zone = server.getPerimeter().getZones().get(camera.getCamera().getZoneId());
        zones = new ArrayList<Zone>(camera.getServer().getPerimeter().getZones().values());
        Collections.sort(zones, new Comparator<Zone>() {

            @Override
            public int compare(Zone o1, Zone o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        ArrayList<String> names = new ArrayList<String>();
        for (Zone z : zones) {
            names.add(z.getName());
        }
        zonesComboBox.setModel(new DefaultComboBoxModel(names.toArray()));
        for (Zone z : zones) {
            if (z.getId().equals(camera.getCamera().getZoneId())) {
                zonesComboBox.setSelectedIndex(zones.indexOf(z));
            }
        }
        zonesComboBoxItemStateChanged(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        zonesComboBox = new javax.swing.JComboBox();

        zonesComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        zonesComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                zonesComboBoxItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(zonesComboBox, 0, 376, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(zonesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void zonesComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_zonesComboBoxItemStateChanged
        camera.getCamera().setZoneId(zones.get(zonesComboBox.getSelectedIndex()).getId());
    }//GEN-LAST:event_zonesComboBoxItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox zonesComboBox;
    // End of variables declaration//GEN-END:variables
}
